package com.fg.pro.model.config;

import cn.hutool.core.util.StrUtil;
import com.fg.pro.model.utils.UserIdThread;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;


public class UserIdInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("userId");
        if (StrUtil.isNotBlank(userId)) {
            UserIdThread.setSysUserId(Long.valueOf(userId));
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserIdThread.clearnUserId();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
